﻿using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace tophd.Classes.Helper
{
    public class MyViewEngine : WebFormViewEngine
    {
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            ViewEngineResult result = null;
            HttpRequestBase request = controllerContext.HttpContext.Request;

            HttpResponseBase response = controllerContext.HttpContext.Response;


            string strController =
                controllerContext.Controller.ToString()
                                 .Substring(controllerContext.Controller.ToString().LastIndexOf('.') + 1)
                                 .Replace("Controller", "");
            if (
                File.Exists(
                    HttpContext.Current.Server.MapPath("~/Views/" + strController + "/" + viewName + ".aspx")) ||
                File.Exists(
                    HttpContext.Current.Server.MapPath("~/Views/" + strController + "/Mobile/" + viewName +
                                                       ".aspx")))
            {
                string url = HttpContext.Current.Request.Url.AbsoluteUri;
                if (!url.Contains("ProductBox") && !url.Contains("ProductComment"))
                {
                    HttpContext.Current.Session["CurrentUrlReturn"] = url;
                }
                result = base.FindView(controllerContext, viewName, masterName, useCache);
                //if (UIHelper.GetDeviceID() != 4 || UserAgentIs(controllerContext, "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; <Manufacturer>; <Device> [;<Operator>])"))
                if (IsMobileDevice(HttpContext.Current.Request.UserAgent) || HttpContext.Current.Request.Browser.IsMobileDevice || UIHelper.GetDeviceID() == 4 || UserAgentIs(controllerContext, "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; <Manufacturer>; <Device> [;<Operator>])"))
                {
                    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);
                }

                if (UserAgentIs(controllerContext,
                                "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebkit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16"))
                    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);
                //Nook Color
                if (UserAgentIs(controllerContext,
                                "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7;en-us) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Safari/530.17"))
                    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);

                if (UserAgentIs(controllerContext, "Windows Phone OS 7") || UserAgentIs(controllerContext, "WP7"))
                {
                    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);
                }


                if (UserAgentIs(controllerContext, "iPhone") || UserAgentIs(controllerContext, "Android") ||
                    UserAgentIs(controllerContext, "WebOS"))
                {
                    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);
                }
                else if (UserAgentIs(controllerContext, "iPad"))
                {
                    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);
                }
                else if ((UserAgentIs(controllerContext, "BlackBerry")))
                {
                    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);
                }
                else if ((UserAgentIs(controllerContext, "LG Browser")) ||
                         (UserAgentIs(controllerContext, "LG NetCast")) ||
                         (UserAgentIs(controllerContext, "SmartTV")))
                {
                    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);
                }


                //else if (UIHelper.GetDeviceID() == 9)
                //{
                //    result = base.FindView(controllerContext, "GoBook/Mobile/" + viewName, masterName, useCache);
                //}
                //else if (UIHelper.GetDeviceID() == 13)
                //{
                //    result = base.FindView(controllerContext, "GoBook/Mobile/" + viewName, masterName, useCache);
                //}
                //else if (UIHelper.GetDeviceID() == 14)
                //{
                //    result = base.FindView(controllerContext, "GoBook/Mobile/" + viewName, masterName, useCache);
                //}


                else if (UserAgentIs(controllerContext, "Windows CE") && request.Url.Segments.Length > 1 &&
                         request.Url.Segments[1].ToLower().StartsWith("viewer"))
                {
                    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName,
                                           useCache);
                }

                return result;
            }


            if (UIHelper.GetDeviceID() != 4)
            {
                // default mobile view if no other mobile view has been set
                result = base.FindView(controllerContext, "GoBook/Mobile/" + viewName, masterName, useCache);
            }
            //if (request.Url.Segments.Length > 1 && request.Url.Segments[2].ToLower().Equals("viewer"))
            //    result = base.FindView(controllerContext, "Mobile/WinCE/" + viewName, masterName, useCache);
            //else
            //    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);

            return result;
        }
        
        public bool UserAgentIs(ControllerContext controllerContext, string userAgentToTest)
        {
            return (controllerContext.HttpContext.Request.UserAgent.IndexOf(userAgentToTest,
                                                                            StringComparison.OrdinalIgnoreCase) >= 0);
        }

        public bool chkInDeviceList(string[] arr, string test)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if (!String.IsNullOrEmpty(arr[i]))
                {
                    if (arr[i].ToLower().Contains(test.ToLower()))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        private string[] mobileDevices = new string[] {"iphone","ppc",
                                                      "windows ce","blackberry",
                                                      "opera mini","mobile","palm",
                                                      "portable","opera mobi" };

        public bool IsMobileDevice(string userAgent)
        {
            userAgent = userAgent.ToLower();
            return mobileDevices.Any(x => userAgent.Contains(x));
        }
    }
}